const redisConfig = require('../config/config.js').redisConfig;
/**
 * redisHelper工具类(适应场景，保持回话，开启一个redis不关闭
 * 可以用连接池改进，只维护一条redis连接不安全，托管给连接池(测试网络情况、保持连接始终可用、释放机制)去维护连接
 * @type {Function}
 * lifuyuan
 * 
*/
module.exports = redisHelper;
function redisHelper(){
    this.client.on('connect',(err)=>{
        if(error){
            console.log('redis connect error...\n %s',error);
        }else{
            console.log('redis connect success...');
        }
    })
}
//redisHelper.prototype.redis = require('redis')
//此处不要用this.redis来创建redis客户端，因为这里的this指向了module，并未指向redisHelper

redisHelper.prototype.client = require('redis').createClient(redisConfig);
redisHelper.prototype.expire = 24 * 60 * 60;//过期时间，默认为一天

/**
 * 封装redis的set方法
 * @param key 键名
 * @param value 值
 * @param expire过期时间
 * @param callback
*/
redisHelper.prototype.set = function(key,value,expire,callback){
    let that = this;
    let size = arguments.length;
    if(size ===3 && typeof expire === 'function'){
        //没传expire参数情况
        callback = expire;
    }
    that.client.set(key,value,function(error,reply){
        if(size === 4 && typeof expire === 'number'){
            console.log('key expire: ' + expire)
            that.client.expire(key,expire);
        }else if(size === 4){
            console.log('key expire: ' + that.expire);
            that.client.expire(key,that.expire);//持续一天
        }
        callback(error,reply)
    })
}
/**
 * 封装redis的hset方法
 * @param hashTble hash字典
 * @param key键名
 * @param value 值
 * @param expire过期时间
 * @param callback
 * 
 * 
*/
redisHelper.prototype.hset = function(hashTable,key,value,expire,callback){
    let that  = this;
    let size = arguments.length;
    if(size === 4 && typeof expire === 'function'){
        //没传expire参数情况
        callback = expire;
    }
    that.client.hset(hashTable,key,value,function(error,reply){
        if(size === 5 && typeof expire === 'number'){
            console.log('key expire ',expire);
            that.client.expire(key,expire);//持久时间
        }else if(size === 5 && typeof expire !== 'number' && expire!== 'forever'){
            console.log('key expire: ' + that.expire);
            that.client.expire(key,that.expire);//持久一天
        }
        callback(error,reply);
    })
}
/**
 * 封装redis的hget方法
 * @param hashTble hash 字典
 * @param key 键值
 * @param callback
 * 
 * 
*/
redisHelper.prototype.hget = function(hashTable,key,callback){
    this.client.hget(hashTable,key,function(error,reply){
        callback(error,reply);
    })
}
/**
 * 封装redis的get方法
 * @param key 需要获取的值的键名
 * @param callback
 * 
*/
redisHelper.prototype.get = function(key,callback){
    this.client.get(key,function(error,reply){
        callback(error,reply);
    })
}
/**
 * 封装redis的del方法
 * @param key键名
 * @param callback
 * 
*/
redisHelper.prototype.delete = function(key,callback){
    let cient = this.client;
    client.del(key,function(error,reply){
        //日志
        // if(error){
        //     const mongodbObj = new thisObj.Mongodb();
        //     let logJson = {
        //         'log':'redis del 出错',
        //         'param':'',
        //         'code':error.code,
        //         'fatal':error.fatal,
        //         'message':error.message,
        //         'create_time':thisObj.customeFunc.getNowFormatTime()
        //     };
        //     mongodbObj.insert('syslog',logJson,function(error,result){});
        // }
        callback(error,reply);
    })
}
/**
 * 封装redis的hdel方法
 * @param key 键名
 * @param callback
 * 
*/
redisHelper.prototype.hdel = function(hashTable,key,callback){
    let client = this.client;
    client.hdel(hashTable,key,function(error,reply){
        callback(error,reply)
    })
}

/**
 * 封装redis的quit方法
 * 
 * 
*/
redisHelper.prototype.close = function(){
    let client = this.client;
    client.on('end',function(error){
        if(error){
            console.log('redis close error');
        }else{
            console.log('redis closed')
        }
    })
}














